﻿using Sage.Data.Extensions;
using System.Text;

namespace Sage.Encryption
{
    /// <summary>
    /// 提供加密服务的扩展方法
    /// </summary>
    public static class EncryptionExtensions
    {
        ///// <summary>
        ///// 将字节数组转换为Base64字符串
        ///// </summary>
        ///// <param name="bytes">要转换的字节数组</param>
        ///// <returns>Base64编码的字符串</returns>
        //public static string ToBase64String(this byte[] bytes) =>
        //    Convert.ToBase64String(bytes);

        ///// <summary>
        ///// 将Base64字符串转换为字节数组
        ///// </summary>
        ///// <param name="base64String">Base64编码的字符串</param>
        ///// <returns>解码后的字节数组</returns>
        //public static byte[] FromBase64String(this string base64String) =>
        //    Convert.FromBase64String(base64String);

        ///// <summary>
        ///// 将字节数组转换为十六进制字符串
        ///// </summary>
        ///// <param name="bytes">要转换的字节数组</param>
        ///// <returns>十六进制编码的字符串</returns>
        //public static string ToHexString(this byte[] bytes) =>
        //    Convert.ToHexString(bytes);

        ///// <summary>
        ///// 将十六进制字符串转换为字节数组
        ///// </summary>
        ///// <param name="hexString">十六进制编码的字符串</param>
        ///// <returns>解码后的字节数组</returns>
        //public static byte[] FromHexString(this string hexString) =>
        //    Convert.FromHexString(hexString);

        /// <summary>
        /// 使用Base64编码加密字符串
        /// </summary>
        /// <param name="encryption">加密服务实例</param>
        /// <param name="plainText">要加密的明文</param>
        /// <returns>Base64编码的密文</returns>
        public static string EncryptToBase64String(this IEncryptionService encryption, string plainText) =>
            encryption.Encrypt(plainText).ToBase64String();

        /// <summary>
        /// 使用十六进制编码加密字符串
        /// </summary>
        /// <param name="encryption">加密服务实例</param>
        /// <param name="plainText">要加密的明文</param>
        /// <returns>十六进制编码的密文</returns>
        public static string EncryptToHexString(this IEncryptionService encryption, string plainText) =>
            encryption.Encrypt(plainText).ToHexString();

        /// <summary>
        /// 使用Base64编码异步加密字符串
        /// </summary>
        /// <param name="encryption">加密服务实例</param>
        /// <param name="plainText">要加密的明文</param>
        /// <returns>表示异步操作的任务，包含Base64编码的密文</returns>
        public static async Task<string> EncryptToBase64StringAsync(this IEncryptionService encryption, string plainText)
        {
            ArgumentException.ThrowIfNullOrEmpty(plainText);
            byte[] encryptedBytes = await encryption.EncryptAsync(Encoding.UTF8.GetBytes(plainText));
            return encryptedBytes.ToBase64String();
        }

        /// <summary>
        /// 使用十六进制编码异步加密字符串
        /// </summary>
        /// <param name="encryption">加密服务实例</param>
        /// <param name="plainText">要加密的明文</param>
        /// <returns>表示异步操作的任务，包含十六进制编码的密文</returns>
        public static async Task<string> EncryptToHexStringAsync(this IEncryptionService encryption, string plainText)
        {
            ArgumentException.ThrowIfNullOrEmpty(plainText);
            byte[] encryptedBytes = await encryption.EncryptAsync(Encoding.UTF8.GetBytes(plainText));
            return encryptedBytes.ToHexString();
        }

        /// <summary>
        /// 从Base64字符串解密
        /// </summary>
        /// <param name="encryption">加密服务实例</param>
        /// <param name="base64Cipher">Base64编码的密文</param>
        /// <returns>解密后的明文</returns>
        public static string DecryptFromBase64String(this IEncryptionService encryption, string base64Cipher) =>
            encryption.DecryptToString(base64Cipher.FromBase64ToBytes());

        /// <summary>
        /// 从十六进制字符串解密
        /// </summary>
        /// <param name="encryption">加密服务实例</param>
        /// <param name="hexCipher">十六进制编码的密文</param>
        /// <returns>解密后的明文</returns>
        public static string DecryptFromHexString(this IEncryptionService encryption, string hexCipher) =>
            encryption.DecryptToString(hexCipher.FromHexToBytes());

        /// <summary>
        /// 从Base64字符串异步解密
        /// </summary>
        /// <param name="encryption">加密服务实例</param>
        /// <param name="base64Cipher">Base64编码的密文</param>
        /// <returns>表示异步操作的任务，包含解密后的明文</returns>
        public static async Task<string> DecryptFromBase64StringAsync(this IEncryptionService encryption, string base64Cipher)
        {
            ArgumentException.ThrowIfNullOrEmpty(base64Cipher);
            byte[] decryptedBytes = await encryption.DecryptAsync(base64Cipher.FromBase64ToBytes());
            return Encoding.UTF8.GetString(decryptedBytes);
        }

        /// <summary>
        /// 从十六进制字符串异步解密
        /// </summary>
        /// <param name="encryption">加密服务实例</param>
        /// <param name="hexCipher">十六进制编码的密文</param>
        /// <returns>表示异步操作的任务，包含解密后的明文</returns>
        public static async Task<string> DecryptFromHexStringAsync(this IEncryptionService encryption, string hexCipher)
        {
            ArgumentException.ThrowIfNullOrEmpty(hexCipher);
            byte[] decryptedBytes = await encryption.DecryptAsync(hexCipher.FromHexToBytes());
            return Encoding.UTF8.GetString(decryptedBytes);
        }
    }

}
